	<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page  import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>
<%@ page import = "java.util.Date" %>
<%@ page import = "java.text.SimpleDateFormat" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Enrollment Detail</title>
<link rel="stylesheet" href="css/stylev2.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<link rel="stylesheet" href="css/redmond/jquery-ui.css" type="text/css"/>

	<style type="text/css">
		a {
	      text-decoration:none;
	   }
	</style>

</head>
<body>
<%
	// get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	

	// check session
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
	//get permission 
	
	String name = String.valueOf(session.getAttribute("name"));
	String surname = String.valueOf(session.getAttribute("surname"));
	String role_name = String.valueOf(session.getAttribute("enrollment_role"));
	
	String department_id = String.valueOf(session.getAttribute("department_id"));

	
			
	System.out.println("name : " + name);///////////////////////////////
	System.out.println("department_id : " + department_id);/////////////////////////
	
	
	List<String> system_name = new ArrayList<String>();
	
	Permission permission = new Permission(strUser,name);
	permission.setDepartmentId(department_id);
	permission.setRegisCreate(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_create"))));
	permission.setRegisDelete(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_delete"))));
	permission.setRegisRead(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_read"))));
	permission.setRegisUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_update"))));
	
	String permiss = "";
	if(permission.getRegisCreate()) {
		permiss = permiss + "Add";
	}
	if(permission.getRegisUpdate()) {
		if("".equals(permiss)) {
			permiss = permiss + "Edit";
		} else {
			permiss = permiss + " Edit";
		}
	}
	if(permission.getRegisRead()) {
		if("".equals(permiss)) {
			permiss = permiss + "Read";
		} else {
			permiss = permiss + " Read";
		}
	}
	if(permission.getRegisDelete()) {
		if("".equals(permiss)) {
			permiss = permiss + "Delete";
		} else {
			permiss = permiss + " Delete";
		}
	}
	
	//check permission
	if(!permission.getRegisRead()){%>
		<script>alert("This user dont have PERMISSION to access this zone.");</script>
		<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=enroll_home.jsp"> 	
	<%}
	
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);

		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
	} catch (Exception e) { 
		out.println(e);  
	}

	System.out.println("hostProperties : "+hostProps);////////////////try/////////////////
	System.out.println("usernameProperties : "+usernameProps);////////////////try/////////////////
	System.out.println("passwordProperties : "+passwordProps);////////////////try/////////////////
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/AIMS" +
				"?user=" + usernameProps  + "&password=" + passwordProps );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}

	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
	
	/*Get basic info in DB*/
	String student_id = request.getParameter("student_id");//GET STUDENT_ID
	System.out.println("student_id= "+student_id);	
	
	List<Course> course = new ArrayList<Course>();
	List<CClass> cclass = new ArrayList<CClass>();
	List<Enrollment> enrollment = new ArrayList<Enrollment>();
	List<Student> student = new ArrayList<Student>();
	// use course_name/*
	
	try{
		ResultSet rs_info = connect.createStatement().executeQuery("SELECT DISTINCT E.student_id, E.class_id, E.grade, E.type_of_enrollment,"
																		+ "L.course_id, L.year, L.semester,"
																		+ "C.course_id, C.course_name_en, C.credit,"
																		+ "S.student_id, S.start_date, S.end_date"
				+ " FROM course_name C, class L, enrollment E, student S "
				+ " WHERE E.student_id="+student_id+" AND E.class_id = L.class_id AND L.course_id = C.course_id AND E.student_id = S.student_id");
		
		while(rs_info.next()) {
			Course cour = new Course();			
			cour.setCourseId(rs_info.getString("C.course_id"));
			cour.setCourseNameEN(rs_info.getString("C.course_name_en"));
			cour.setCredit(rs_info.getInt("C.credit"));			
			
			course.add(cour);			
			System.out.println("course.size(): "+course.size());	
			
			CClass cclas = new CClass();			
			cclas.setCourse_id(rs_info.getString("L.course_id"));
			cclas.setYear(rs_info.getString("L.year"));
			cclas.setSemester(rs_info.getString("L.semester"));
			
			cclass.add(cclas);
			System.out.println("cclass.size(): "+cclass.size());
			
			Enrollment enroll = new Enrollment();
			
			enroll.setStudent_id(rs_info.getString("E.student_id"));
			enroll.setClass_id(rs_info.getInt("E.class_id"));
			enroll.setGrade(rs_info.getString("E.grade"));
			enroll.setType(rs_info.getString("E.type_of_enrollment"));
			
			enrollment.add(enroll);
			System.out.println("enrollment.size(): "+enrollment.size());
			
			Student stud = new Student();
			
			stud.setStudentId(rs_info.getString("S.student_id"));
			stud.setStart_date(rs_info.getString("S.start_date"));
			stud.setEnd_date(rs_info.getString("S.end_date"));
			
			student.add(stud);
			System.out.println("student.size(): "+student.size());
		}
		
	}catch (SQLException e) {
		e.printStackTrace();
	}	
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	
	Date date = new Date();
	int current_year = date.getYear()+1900;
	int current_month = date.getMonth()+1;
	Student stu = new Student();
	

	ResultSet rs = 
			connect.createStatement().executeQuery(" SELECT S.picture, N.title_th ,"+
					" N.name_th, N.surname_th , N.name_en , N.surname_en ,P.personal_id, P.gender,P.marital_status ,"+
				    " P.address , P.phone_number, P.email , P.facebook , S.father_name , S.father_address ,"+
					" S.father_phone ,S.mother_name , S.mother_address , S.mother_phone , S.parent_name,"+
				    " S.parent_address ,S.parent_phone ,S.student_id, S.start_date , S.entrance_id  , S.school ,"+
					" S.gpa , ST.status_name ,D.department_name_th, S.curriculum_id ,P.date_of_birth ,S.lecturer_id "+
				    " FROM student S, full_name N, department D, status ST, people P "+
				    " WHERE S.index = P.index AND P.full_name_id = N.full_name_id "+
				    " AND S.department_id = D.department_id AND S.status_id = ST.status_id "+
				    " AND S.student_id='"+student_id+"'");
	while(rs.next())
	{
		//stu.setPicture(rs.getString("picture"));
		String src_path_full = rs.getString("picture");		if((src_path_full!=null)&&(!("null").equals(src_path_full)))

		{
			String[] src_path_temp = src_path_full.split("/");	
			int length = src_path_temp.length;
			
			String src_path_a = src_path_temp[length-2];
			String src_path_b = src_path_temp[length-1];
			
			String src_path = src_path_a+"/"+src_path_b;
			
			stu.setPicture(src_path);
		
		}else{
			stu.setPicture("upload/default_person_icon.jpg");
			
		}		
		
		stu.setNameTH(rs.getString("name_th"));
		stu.setSurnameTH(rs.getString("surname_th"));
		Date start_date = new SimpleDateFormat("yyyy-MM-dd").parse(rs.getString("start_date"));
		int start_date_num = start_date.getYear()+1900;
		String year_str ;
	
	
		if(current_month<6)
		{
			
			year_str =(current_year-start_date_num) + "";
			
		}else{
		
			year_str =(current_year-start_date_num+1)+"";
		}
		
		stu.setYear(year_str);
		stu.setDepartment(rs.getString("department_name_th"));
		stu.setStudentId(rs.getString("student_id"));
	}
	
	
	
%>



<div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser%>&nbsp;&nbsp;</font></label></p>
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>
				</tr>
			</table>			
				
				<div id="main-content-ac">
										</br></br>
					<div id="top-content" style="background-color: #e2e0e0;">
						</br></br>
							<center>
								<table  style="font-size:20px; background-color: #f2f0f0;" >
									<tr>
										<td width="50" height="10" rowspan="2" style="text-align: center;">
											 <% 
											  
											  	System.out.println("PICTURE:"+stu.getPicture());
												if(stu.getPicture()==null)
												{
														System.out.println(" PICTURE NULL");
											  %>
													 
											 			<img id="student_image" src="upload/default_person_icon.jpg" style="width:130px;height:160px;	">
											  <% 													
												}
												else{
													System.out.println(" PICTURE NOT NULL");
													
											   %>					
														 <img id="student_image" src="<%=stu.getPicture() %>" style="width:130px;height:160px;">
											   <%
												}
											  
											  %>					        		
						        		
						        	
										</td>
										 <td height="10">
									             	
									                	<%=stu.getNameTH()+"  "+stu.getSurnameTH() %>					          		
									     </td>
									     <td height ="10">
									     			<%=stu.getDepartment() %>
									     </td>
									</tr>
									<tr>
										<td height="10">
													<%=stu.getStudentId() %>
										</td>
										 <td height="10">
									               ชั้นปี <%=stu.getYear() %>
									     </td>
										
									</tr>
								     
								</table>
							</center>
							</br></br>
							
					</div>					
					<br><br>
					
				<!-- Course-Regis part -->
	<div id="top-content">
	<!-- START -->
                <div id="divshowLoadGPA">
                    <table id="tbShowgpa" width="100%" cellpadding="0" cellspacing="1" style="border: 1px solid #c5dbec;font-size:14px;">
                        <tbody><tr class="ui-widget-header title4TD" align="center" style="border: 1px solid #c5dbec;">

                            <td width="10%" rowspan="2" colspan="2" class="title5TD">
                            Semester
                            </td>
                            <td width="10%" rowspan="2" class="title5TD">
                            Number Of Courses
                            </td>
                            <td width="40%" colspan="4" class="title5TD">
                            Semester
                            </td>
                            <td width="40%" colspan="4" class="title5TD">
                            Cumulative
                            </td>
                        </tr>
                        <tr align="center" class="ui-widget-header title4TD" >
                            <td width="10%" class="title5TD">
                            Credits<br>Registered
                            </td>
                            <td width="11%" class="title5TD">
                            Credits<br>Earned
                            </td>
                            <td width="11%" class="title5TD">
                            Credits<br>Compute
                            </td>
                            <td width="10%" class="title5TD">
                            GPA
                            </td> 
                            <td width="10%" class="title5TD">
                            Credits<br>Registered
                            </td>
                            <td width="11%" class="title5TD">
                            Credits<br>Earned
                            </td>
                            <td width="11%" class="title5TD">
                            Credits<br>Compute
                            </td>
                            <td width="10%" class="title5TD">
                            GPA</td>
                        </tr>
               
               <!-- PART 1 -->    
               <%
               		
               		            ////////////////////// QUERY LAST YEAR INFO ///////////////////////////////////
               		
               					List<CClass> cclass3 = new ArrayList<CClass>();// Query Class for each semester
                        				try{
         						ResultSet rs_semester2 = connect.createStatement().executeQuery("SELECT DISTINCT E.student_id, E.class_id, E.grade, E.type_of_enrollment,"
         																					+ "L.course_id, L.year, L.semester,"
         																					+ "C.course_id, C.course_name_en, C.credit,"
         																					+ "S.student_id, S.start_date, S.end_date"
         								+ " FROM course_name C, class L, enrollment E, student S "
         								+ " WHERE E.student_id="+student_id+" AND E.class_id = L.class_id AND L.course_id = C.course_id AND E.student_id = S.student_id"
         								+ " ORDER BY L.year DESC");

         						
         						while(rs_semester2.next()) {
         							         							
         							CClass cclas3 = new CClass();			
         							cclas3.setCourse_id(rs_semester2.getString("L.course_id"));
         							cclas3.setYear(rs_semester2.getString("L.year"));
         							cclas3.setSemester(rs_semester2.getString("L.semester"));
         							
         							cclass3.add(cclas3);
         							System.out.println("cclass3.size(): "+cclass3.size());         		
         							
         						}
         						
         					}catch (SQLException e) {
         						e.printStackTrace();
         					}
               					
        /////////////////////////////////////////////////////
        List<Student> student5 = new ArrayList<Student>();
        try{
		ResultSet rs_student = connect.createStatement().executeQuery("SELECT `student_id`,`start_date`, `end_date`"
				+ " FROM student S "
				+ " WHERE student_id="+student_id);
		
		while(rs_student.next()) {		
			
			Student stud5 = new Student();
			
			stud5.setStudentId(rs_student.getString("S.student_id"));
			stud5.setStart_date(rs_student.getString("S.start_date"));
			stud5.setEnd_date(rs_student.getString("S.end_date"));
			
			student5.add(stud5);
			System.out.println("student5.size(): "+student5.size());
		}
		
	}catch (SQLException e) {
		e.printStackTrace();
	}
                    ////////////////////////GET BASIC VARIABLE/////////////////
                    String tempStartYear = student5.get(0).getStart_date();
               		System.out.println("tempStartYear: "+tempStartYear);
               		String partStart[] = tempStartYear.split("-");
               		int StartYear = Integer.parseInt(partStart[0])+543;
               		System.out.println("StartYear: "+StartYear);
               		int EndYear;
               		
               	if(cclass3.size() == 0){
               		
               		EndYear = StartYear-1;
               		System.out.println("EndYear: "+EndYear);   
               		
               	}else{
               		String tempEndYear = cclass3.get(0).getYear();
               		System.out.println("tempEndYear: "+tempEndYear);
               		String partEnd[] = tempEndYear.split("-");
               		EndYear = Integer.parseInt(partEnd[0])+543;
               		System.out.println("EndYear: "+EndYear);    
               	}
               		int i = 0;
               		int semester = 1;
               		int checker = 0; 
               		
               		// Initial FOR CUMULATIVE
               		int totalCredit = 0;
       				int totalCreditCom = 0;
       				double TotalGPA = 0;
       				int OLDCompute = 0;
            		
               		for(int local = StartYear;local<=EndYear;local++){//Loop For Year
               			System.out.println("local: "+local);
						/////////////////////////////////////////////////////////////////////////////////////////////
						////////////////////////////////////QUERY SEMESTER///////////////////////////////////////////
						List<CClass> cclass_semester = new ArrayList<CClass>();// Query Class for each semester
               			try{
     						ResultSet rs_semester_1 = connect.createStatement().executeQuery("SELECT DISTINCT E.student_id, E.class_id, E.grade, E.type_of_enrollment,"
     																					+ "L.course_id, L.year, L.semester,"
     																					+ "C.course_id, C.course_name_en, C.credit,"
     																					+ "S.student_id, S.start_date, S.end_date"
     								+ " FROM course_name C, class L, enrollment E, student S "
     								+ " WHERE E.student_id="+student_id+" AND L.year="+(local-543)+" AND E.class_id = L.class_id AND L.course_id = C.course_id AND E.student_id = S.student_id"
     								+ " ORDER BY L.semester DESC");
     						while(rs_semester_1.next()) {
      							
     							CClass cclas_semester = new CClass();			
     							cclas_semester.setCourse_id(rs_semester_1.getString("L.course_id"));
     							cclas_semester.setYear(rs_semester_1.getString("L.year"));
     							cclas_semester.setSemester(rs_semester_1.getString("L.semester"));
     							
     							cclass_semester.add(cclas_semester);
     							System.out.println("cclass_semester.size(): "+cclass_semester.size());         		
     							
     						}
               			}catch (SQLException e) {
     						e.printStackTrace();
     					}
						int tempEndSemester = 0;
						if(cclass_semester.size()==0){
							int EndSemester = 3;
							tempEndSemester = EndSemester;
						}else{
	               			int EndSemester = Integer.parseInt(cclass_semester.get(0).getSemester());
	               			tempEndSemester = EndSemester;
	 	               		System.out.println("EndSemester: "+EndSemester);
						}
						
               			for(semester=1;semester<=tempEndSemester;semester++){// Loop For Semester
               			// Initial FOR SEMESTER
               				int totalCreditSemester = 0; //credit earned
               				int totalCreditComSemester = 0;	// credit compute
               				int totalCreditEarn = 0;
               				double SemesterGPA = 0;
               				System.out.println("/////////////////START//////////////");
               				System.out.println("SemesterGPA: "+SemesterGPA);
                        /////////////////////////////////////////////////////////////////////////////////////////////
                        /////////////////////////////////////////START///////////////////////////////////////////////
                        		int enrollCount = 1;
                        		                       		
                        		List<CClass> cclass2 = new ArrayList<CClass>();// Query Class for each semester
                        		List<Enrollment> enrollment2 = new ArrayList<Enrollment>();
                        		List<Course> course2 = new ArrayList<Course>();
                        		List<Student> student2 = new ArrayList<Student>();
                        		
                        				try{
         						ResultSet rs_semester = connect.createStatement().executeQuery("SELECT DISTINCT E.student_id, E.class_id, E.grade, E.type_of_enrollment,"
         																					+ "L.course_id, L.year, L.semester,"
         																					+ "C.course_id, C.course_name_en, C.credit,"
         																					+ "S.student_id, S.start_date, S.end_date"
         								+ " FROM course_name C, class L, enrollment E, student S "
         								+ " WHERE E.student_id="+student_id+" AND L.year="+(local-543)+" AND L.semester="+semester+" AND E.class_id = L.class_id AND L.course_id = C.course_id AND E.student_id = S.student_id"
         								+ " ORDER BY E.class_id");

         						
         						while(rs_semester.next()) {
         							Course cour2 = new Course();			
         							cour2.setCourseId(rs_semester.getString("C.course_id"));
         							cour2.setCourseNameEN(rs_semester.getString("C.course_name_en"));
         							cour2.setCredit(rs_semester.getInt("C.credit"));			
         							
         							course2.add(cour2);			
         							System.out.println("course2.size(): "+course2.size());	
         							
         							CClass cclas2 = new CClass();			
         							cclas2.setCourse_id(rs_semester.getString("L.course_id"));
         							cclas2.setYear(rs_semester.getString("L.year"));
         							cclas2.setSemester(rs_semester.getString("L.semester"));
         							
         							cclass2.add(cclas2);
         							System.out.println("cclass2.size(): "+cclass2.size());
         							
         							Enrollment enroll2 = new Enrollment();
         							
         							enroll2.setStudent_id(rs_semester.getString("E.student_id"));
         							enroll2.setClass_id(rs_semester.getInt("E.class_id"));
         							enroll2.setGrade(rs_semester.getString("E.grade"));
         							enroll2.setType(rs_semester.getString("E.type_of_enrollment"));
         							
         							enrollment2.add(enroll2);
         							System.out.println("enrollment2.size(): "+enrollment2.size());
         							
         							Student stud2 = new Student();
         							
         							stud2.setStudentId(rs_semester.getString("S.student_id"));
         							stud2.setStart_date(rs_semester.getString("S.start_date"));
         							stud2.setEnd_date(rs_semester.getString("S.end_date"));
         							
         							student2.add(stud2);
         							System.out.println("student2.size(): "+student2.size());
         						}
         						
         					}catch (SQLException e) {
         						e.printStackTrace();
         					}
               /////////////////////////////////Calculate Grade///////////////////////////
                        for(int temp = 0;temp<enrollment2.size();temp++){
                        	double tempGrade = 0;
                        	if(enrollment2.get(temp).getGrade().toUpperCase().equals("A") && (enrollment2.get(temp).getType().equals("N") || enrollment2.get(temp).getType().equals("A"))){
                        		tempGrade = 4*course2.get(temp).getCredit();                       		
                        	}else if(enrollment2.get(temp).getGrade().toUpperCase().equals("B+") && (enrollment2.get(temp).getType().equals("N") || enrollment2.get(temp).getType().equals("A"))){
                        		tempGrade = 3.5*course2.get(temp).getCredit();
                        	}else if(enrollment2.get(temp).getGrade().toUpperCase().equals("B") && (enrollment2.get(temp).getType().equals("N") || enrollment2.get(temp).getType().equals("A"))){
                        		tempGrade = 3*course2.get(temp).getCredit();
                        	}else if(enrollment2.get(temp).getGrade().toUpperCase().equals("C+") && (enrollment2.get(temp).getType().equals("N") || enrollment2.get(temp).getType().equals("A"))){
                        		tempGrade = 2.5*course2.get(temp).getCredit();
                        	}else if(enrollment2.get(temp).getGrade().toUpperCase().equals("C") && (enrollment2.get(temp).getType().equals("N") || enrollment2.get(temp).getType().equals("A"))){
                        		tempGrade = 2*course2.get(temp).getCredit();
                        	}else if(enrollment2.get(temp).getGrade().toUpperCase().equals("D+") && (enrollment2.get(temp).getType().equals("N") || enrollment2.get(temp).getType().equals("A"))){
                        		tempGrade = 1.5*course2.get(temp).getCredit();
                        	}else if(enrollment2.get(temp).getGrade().toUpperCase().equals("D") && (enrollment2.get(temp).getType().equals("N") || enrollment2.get(temp).getType().equals("A"))){
                        		tempGrade = 1*course2.get(temp).getCredit();
                        	}else if(enrollment2.get(temp).getGrade().toUpperCase().equals("F") && (enrollment2.get(temp).getType().equals("N") || enrollment2.get(temp).getType().equals("A"))){
                        		tempGrade = 0*course2.get(temp).getCredit();
                        	}
                        	SemesterGPA = SemesterGPA + tempGrade ;
                        	System.out.println("SemesterGPA= "+SemesterGPA);
                        }
               
               		
                %>       
                    <tr class="uiElementTD" align="center" onmouseover="setColor(this,'#FFCCCC');" onmouseout="setColor(this,'white');" style="cursor:pointer;" bgcolor="white">  
                       <td></td>
                       <td class="ui-GPA-detail ui-state-default" width="7%">
                        <%=semester%>/<%=local%> 
                        </td>
                        <td class="ui-GPA-detail" width="10%">
                        <%if(local == 2556 && semester == 3 && stu.getYear().equals("4")){%>
                        	1
                        <%}else{   
                        %>                        
                        <%=enrollment2.size() %>
                        <%} %>
                        </td>
                        <td class="ui-GPA-detail" width="10%">
                        <%
                        if(local == 2556 && semester == 3 && stu.getYear().equals("4")){                        	                     	
		                        	totalCreditSemester = totalCreditSemester + 0; 
		                        	totalCredit = totalCredit + 0; 
		                        	System.out.println("totalCreditSemester= "+totalCreditSemester);	                        	
                        	
                        }else{
	                        for(int temp = 0;temp<course2.size();temp++){     
	                        	
	                        	if(!enrollment2.get(temp).getGrade().equals("P") ){                        	
		                        	totalCreditSemester = totalCreditSemester + course2.get(temp).getCredit(); 
		                        	totalCredit = totalCredit + course2.get(temp).getCredit(); 
		                        	totalCreditEarn = totalCreditEarn + course2.get(temp).getCredit();
		                        	System.out.println("totalCreditSemester= "+totalCreditSemester);
	                        	}else{                        		
	                        	}
	                       	 }
                        }
                        %>
                        <%=totalCreditSemester%>                        
                        </td>
                        <td class="ui-GPA-detail" width="10%">     
                        <%for(int temp = 0;temp<course2.size();temp++){
                        	
                        	System.out.println("check 1  "+(enrollment2.get(temp).getType().equals("A") || enrollment2.get(temp).getType().equals("N")));
                        	System.out.println("check 1  "+((enrollment2.get(temp).getType().equals("A") || enrollment2.get(temp).getType().equals("N")) && !enrollment2.get(temp).getGrade().equals("X")));
                        	
                        	if((enrollment2.get(temp).getType().equals("A") || enrollment2.get(temp).getType().equals("N")) && !enrollment2.get(temp).getGrade().equals("X") && !enrollment2.get(temp).getGrade().equals("P") && !enrollment2.get(temp).getGrade().equals("S") && !enrollment2.get(temp).getGrade().equals("T") && !enrollment2.get(temp).getGrade().equals("U") && !enrollment2.get(temp).getGrade().equals("W")){
                        		totalCreditComSemester = totalCreditComSemester + course2.get(temp).getCredit();
                        		totalCreditCom = totalCreditCom + course2.get(temp).getCredit(); 
                        	}else{
                        	}
                        	System.out.println("******************totalCreditComSemester= "+totalCreditComSemester);
                        	System.out.println("******************totalCreditCom= "+totalCreditCom);
                        %>
                        <%} %>                   
                        <%=totalCreditComSemester%>     
                        </td>
                        <td class="ui-GPA-detail" width="10%">                        
                        <%=totalCreditComSemester%>     
                        </td>
                        <td class="ui-GPA-detail" width="10%">                        
                        <%
                        double GradeResult = SemesterGPA/totalCreditComSemester; // Grade in Semester
                        
                        if(Double.isNaN(GradeResult)){%>
                        	0.0
                        <%}else{
                        	GradeResult = Math.floor(GradeResult * 1000) / 1000;
                        %>
                        	<%= GradeResult%>
                        <%} %>	
                        </td>
                        
                        <!-- CUMULATIVE SITEs -->
                        <%
	                        
	                   		int GradeCumulative = totalCredit; //for cumu
	                   		int GradeCumulativeCompute = totalCreditCom;//for cumu
	                   		int GradeCumulativeEarn = totalCreditEarn;//for cumu
	                   		int TotalCumuCredit = 0;
	                   		TotalCumuCredit = TotalCumuCredit + GradeCumulative;
	                   		System.out.println("TotalCumuCredit + " +totalCredit+"= "+TotalCumuCredit);
	                   		
	                   		int TotalCumuCompute = 0;
	                   		TotalCumuCompute = TotalCumuCompute + GradeCumulativeCompute;
	                   		System.out.println("TotalCumuCompute+ " +totalCreditCom+"= "+TotalCumuCompute);
							
	                   		//////////////////////////////////////////////////////////////
	                   		System.out.println("GradeResult*= "+GradeResult);	//3.5
	                   		System.out.println("*totalCreditComSemester= "+totalCreditComSemester);//3
	                   		System.out.println("TotalGPA OLD*= "+TotalGPA);//4
	                   		
	                   		double temp_oldGPA = TotalGPA;
	                   		System.out.println("*OLDCompute= "+OLDCompute);//3
	                   		System.out.println("/TotalCumuCompute= "+TotalCumuCompute);//6
	                   		
	                   		TotalGPA = ((GradeResult*totalCreditComSemester)+(TotalGPA*OLDCompute))/TotalCumuCompute;
	                   		System.out.println("///TotalGPA NEW= "+TotalGPA);	
	                   		OLDCompute = OLDCompute	+totalCreditComSemester ;
                        %>
                        <td class="ui-GPA-detail ui-state-default" width="10%">
                        <%=TotalCumuCredit%>  
                        </td>
                        <td class="ui-GPA-detail ui-state-default" width="11%">
                        <%=TotalCumuCompute%>  
                        </td>
                        <td class="ui-GPA-detail ui-state-default" width="11%">
                        <%=TotalCumuCompute%>  
                        </td>
                        <td class="ui-GPA-detail ui-state-default" width="10%">
                        <%
                        if(totalCreditComSemester == 0){
                        	TotalGPA = temp_oldGPA;
                        %>
                        	<%= temp_oldGPA %>	
                        <%}else if(Double.isNaN(TotalGPA)){%>
                        	0.0
                        <%}else{                        
                        	TotalGPA = Math.floor(TotalGPA * 1000) / 1000;                        
                        %>
                        	<%=TotalGPA%>
                        <%} %>	
                        </td>
                    </tr>
                <!-- PART 2 -->                      
                <td colspan="11">
                        <br>
                        <center>                  
                <table width="80%" cellpadding="0" cellspacing="1" style="font-size: 14px;" >
                    <tbody><tr class="ui-widget-header" align="center" height="30px" >
                        <td align="center">
                        No.
                        </td>
                        <td>
                        Subject Name
                        </td>
                        <td>
                        Credits
                        </td>
                        <td>
                        Grade
                        </td>
                        <td>
                        Register Status
                        </td>
                         <td>
                        Delete
                        </td>
                         <td>
                        Edit
                        </td>
                    </tr>
               <%
               		int j = 0;
               		int temp = 1;
               		
               		for(Enrollment enrol : enrollment2){
               			//init               			
               			if(local == 2556 && semester == 3 && stu.getYear().equals("4")){
               				if(temp ==2){
               			%>
               					 <tr align="center" height="0px">          
     		                    <tr align="center">
     		                        <td class="ui-GPA-detail">
     		                        <%=enrollCount%>
     		                        </td>
     		                        <td class="ui-profile-detail" align="Left">&nbsp;		                           
     		                        <%=course2.get(j).getCourseId() %> - <%=course2.get(j).getCourseNameEN()%>			                       
     		                        </td>
     		                        <td class="ui-GPA-detail" align="Center" >
     		                        <%=course2.get(j).getCredit() %>
     		                        </td>
     		                        <td class="ui-GPA-detail" align="Center" >
     		                        <%=enrol.getGrade().toUpperCase() %><br>
     		                        </td>
     		                        <td class="ui-GPA-detail" align="Center" >
     		                        <%=enrol.getType() %><br>
     		                        </td>
     		         <%
     					if(permission.getRegisDelete()){
     				 %>	           
     		     
     		                    <form id="form<%=enrollCount%>" name="form<%=enrollCount%>" method="post" action="enroll_student_enrollment_detail_delete.jsp">	
     			                    <input type="hidden" id="student_id" name="student_id" value="<%=student_id%>" />
     			                    <input type="hidden" id="class_id" name="class_id" value="<%=enrollment2.get(j).getClass_id()%>" />
     			                    <input type="hidden" id="type" name="type" value="<%=enrol.getType()%>" />
     			                    <input type="hidden" id="grade" name="grade" value="<%=enrol.getGrade().toUpperCase()%>" />
     			                    <input type="hidden" id="course_id" name="course_id" value="<%=course2.get(j).getCourseId()%>" />
     			                    <input type="hidden" id="course_name" name="course_name" value="<%=course2.get(j).getCourseNameEN()%>" />
     			                    <input type="hidden" id="checker" name="checker" value="none" />
     			                    <input type="hidden" id="counter" name="counter" value="none" />	
     		                    
     		                    	<input type="hidden" id="credit" name="credit" value="<%=course2.get(j).getCredit() %>" />
     		      					<td><center><span title="ลบข้อมูล"><input type="image" src="image/delete_icon.png" height="30px" width="30px"/></span></center></td>
     							</form>   
     				<%
     					}else{
     				%>
     				 	<td><center>-</center></td>
     				<%	}
     				%>				
     		                 	<form id="form<%=enrollCount%>" name="form<%=enrollCount%>" method="post" action="enroll_student_enrollment_detail_edit.jsp">	
     		                 		<input type="hidden" id="student_id" name="student_id" value="<%=student_id%>" />
     			                    <input type="hidden" id="class_id" name="class_id" value="<%=enrollment2.get(j).getClass_id()%>" />
     			                    <input type="hidden" id="type" name="type" value="<%=enrol.getType()%>" />
     			                    <input type="hidden" id="grade" name="grade" value="<%=enrol.getGrade().toUpperCase()%>" />
     			                    <input type="hidden" id="course_id" name="course_id" value="<%=course2.get(j).getCourseId()%>" />
     			                    <input type="hidden" id="course_name" name="course_name" value="<%=course2.get(j).getCourseNameEN()%>" />
     			                    <input type="hidden" id="checker" name="checker" value="none" />
     			                    <input type="hidden" id="counter" name="counter" value="none" />	
     			                    
     		      					<td><center><span title="แก้ไขข้อมูล"><input type="image" src="image/edit_icon.png" height="30px" width="30px"/></span></center></td>	           
     		                 	</form> 
     		                 	  
     		                 	</tr>
     		                  <%
     		                  		enrollCount++;
     		                  		i++;
     		                  		j++;
               				}    		
               				j++;
               				temp++;
              ///////////////////////////////////////////////////////////////////////////////////////////// 				
               			}else{
               %> 
		            <tr align="center" height="0px">     
		                    <tr align="center">
		                        <td class="ui-GPA-detail">
		                        <%=enrollCount%>
		                        </td>
		                        <td class="ui-profile-detail" align="Left">&nbsp;		                           
		                        <%=course2.get(j).getCourseId() %> - <%=course2.get(j).getCourseNameEN()%>			                       
		                        </td>
		                        <td class="ui-GPA-detail" align="Center" >
		                        <%=course2.get(j).getCredit() %>
		                        </td>
		                        <td class="ui-GPA-detail" align="Center" >
		                        <%=enrol.getGrade().toUpperCase() %><br>
		                        </td>
		                        <td class="ui-GPA-detail" align="Center" >
		                        <%=enrol.getType() %><br>
		                        </td>
		         <%
					if(permission.getRegisDelete()){
				 %>	           
		     
		                    <form id="form<%=enrollCount%>" name="form<%=enrollCount%>" method="post" action="enroll_student_enrollment_detail_delete.jsp">	
			                    <input type="hidden" id="student_id" name="student_id" value="<%=student_id%>" />
			                    <input type="hidden" id="class_id" name="class_id" value="<%=enrollment2.get(j).getClass_id()%>" />
			                    <input type="hidden" id="type" name="type" value="<%=enrol.getType()%>" />
			                    <input type="hidden" id="grade" name="grade" value="<%=enrol.getGrade().toUpperCase()%>" />
			                    <input type="hidden" id="course_id" name="course_id" value="<%=course2.get(j).getCourseId()%>" />
			                    <input type="hidden" id="course_name" name="course_name" value="<%=course2.get(j).getCourseNameEN()%>" />
			                    <input type="hidden" id="checker" name="checker" value="none" />
			                    <input type="hidden" id="counter" name="counter" value="none" />	
		                    
		                    	<input type="hidden" id="credit" name="credit" value="<%=course2.get(j).getCredit() %>" />
		      					<td><center><span title="ลบข้อมูล"><input type="image" src="image/delete_icon.png" height="30px" width="30px"/></span></center></td>
							</form>   
				<%
					}else{
				%>
				 	<td><center>-</center></td>
				<%	}
				%>				
		                 	<form id="form<%=enrollCount%>" name="form<%=enrollCount%>" method="post" action="enroll_student_enrollment_detail_edit.jsp">	
		                 		<input type="hidden" id="student_id" name="student_id" value="<%=student_id%>" />
			                    <input type="hidden" id="class_id" name="class_id" value="<%=enrollment2.get(j).getClass_id()%>" />
			                    <input type="hidden" id="type" name="type" value="<%=enrol.getType()%>" />
			                    <input type="hidden" id="grade" name="grade" value="<%=enrol.getGrade().toUpperCase()%>" />
			                    <input type="hidden" id="course_id" name="course_id" value="<%=course2.get(j).getCourseId()%>" />
			                    <input type="hidden" id="course_name" name="course_name" value="<%=course2.get(j).getCourseNameEN()%>" />
			                    <input type="hidden" id="checker" name="checker" value="none" />
			                    <input type="hidden" id="counter" name="counter" value="none" />	
			                    
		      					<td><center><span title="แก้ไขข้อมูล"><input type="image" src="image/edit_icon.png" height="30px" width="30px"/></span></center></td>	           
		                 	</form> 
		                 	  
		                 	</tr>
		                  <%
		                  		enrollCount++;
		                  		i++;
		                  		j++;
		                  		
		               		}//else
               		}//for
               		
               	%>  
                  </tbody>
               </table>
               </center>    
                    <br>
               </td>              
               <%	
               		}//LOOP SEMESTER
               	}//LOOP YEAR
               	
               	connect.close();
               %>               	
                   </tbody>
                   </table>
                   </div>
                   
			<!-- END -->
			
					<br><br>				
					<a href="enroll_search.jsp"><center><button class="buttonStyle">BACK</button></center></a>							
					<br><br>
	</div>
					
				
				
				
			<div style="clear: both;"></div>				
			<div id="footer"></div>
			
		</div>		
			<div style="clear: both;"></div>		
   </div>
   



</script>
</body>
</html>
